﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Caliburn.Micro;
using WinMote7.Helpers;
using UPnPControllerServices;
using System.Collections.ObjectModel;

namespace WinMote7.ViewModels
{
    public class BrowseMediaViewModel : INotifyPropertyChangedEx
    {
        readonly INavigationService navigationService;

        private Guid serverId;

        public BrowseMediaViewModel(INavigationService navigationService)
        {
            LoadingPanel = true;
            Media = new ObservableCollection<MediaItem>();
            this.navigationService = navigationService;

            LoadParameters(navigationService);

            LoadMediaOnServer();
        }

        private void LoadMediaOnServer()
        {
            UPnPMediaBrowserServiceClient client = new UPnPMediaBrowserServiceClient();
            client.GetAvailableMediaFromServerCompleted += new EventHandler<GetAvailableMediaFromServerCompletedEventArgs>(client_GetAvailableMediaFromServerCompleted);
            client.GetAvailableMediaFromServerAsync(serverId);
        }

        private void LoadParameters(INavigationService navigationService)
        {
            var queryString = QueryStringHelper.ParseQueryString(navigationService.Source.OriginalString);

            string serverName;
            queryString.TryGetValue("ServerName", out serverName);

            ServerName = HttpUtility.UrlDecode(serverName);

            queryString.TryGetValue("uuid", out serverName);
            serverId = new Guid(HttpUtility.UrlDecode(serverName));
        }

        void client_GetAvailableMediaFromServerCompleted(object sender, GetAvailableMediaFromServerCompletedEventArgs e)
        {
            Media.Clear();

            foreach (MediaItem item in e.Result)
            {
                Media.Add(item);
            }

            LoadingPanel = false;
            NotifyOfPropertyChange("LoadingPanel");
            NotifyOfPropertyChange("NoResultsFound");
        }

        public string ServerName { get; set; }

        public ObservableCollection<MediaItem> Media
        {
            get;
            set;
        }

        public void NotifyOfPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        public void PlayMedia(MediaItem from)
        {
            navigationService.Navigate(new Uri(string.Format("/Views/PlayMedia.xaml?serverId={0}&MediaUri={1}&MediaName={2}", HttpUtility.UrlEncode(from.ServerId.ToString()), HttpUtility.UrlEncode(from.UriLocation), HttpUtility.UrlDecode(from.Name)), UriKind.RelativeOrAbsolute));
        }

        public bool LoadingPanel
        {
            get;
            set;
        }

        public bool NoResultsFound
        {
            get
            {
                return Media.Count == 0;
            }
        }
    }
}
